package com.sanri.buildingblock.exportcenter.aop;

import com.sanri.buildingblock.exportcenter.annotation.ExcelExport;
import com.sanri.buildingblock.exportcenter.annotation.FileLocation;
import com.sanri.buildingblock.exportcenter.remote.TaskRuntime;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Map;

/**
 * @description:
 * @author: huangzhengri
 * @time: 2023-04-17 10:11
 */
@Aspect
@Component
public class StartExportInterceptor {

    @Autowired
    private TaskRuntime taskRuntime;

    @Pointcut("@annotation(com.sanri.buildingblock.exportcenter.annotation.ExcelExport)")
    public void pointcut(){}

    /**
     * 启动导出时, 插入一个实例
     * TODO 后面迭代需要保证连接一定是可用的
     */
    @Before("pointcut()")
    public void before(JoinPoint joinPoint){
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        ExcelExport excelExport = AnnotatedElementUtils.findMergedAnnotation(method, ExcelExport.class);
        String uniqueId = excelExport.uniqueId();
        FileLocation fileLocation = excelExport.fileLocation();

        String startInstance = taskRuntime.startInstance(uniqueId, null);
    }

    public void afterThrow(){

    }
}
